package com.arofoxmsn.services.msj;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.arofoxmsn.model.ScopeArea;
import com.arofoxmsn.model.User;
import com.arofoxmsn.model.location.UserLocation;
import com.arofoxmsn.model.message.Message;
import com.arofoxmsn.repository.ScopeAreaTable;
import com.arofoxmsn.repository.UserTable;
import com.arofoxmsn.repository.message.MessageTable;
import com.arofoxmsn.utils.FlashMessage;
import com.arofoxmsn.utils.ParametersTool;

public class MessageService {
	
	/**
	 * this method is in charge of loading a message into data base.
	 * It is a call to a web services and it returns.
	 * @param message
	 * @return
	 */
	public static FlashMessage send(Message message){
		FlashMessage flashToReturn = new FlashMessage();
		if(ParametersTool.testNullObjectBoolean(message)){
			MessageTable table = new MessageTable();
			table.save(message);
			flashToReturn.setData(FlashMessage.Type.OK, "Message will be sent");
		}else{
			flashToReturn.setData(FlashMessage.Type.ERROR, "Message can't be null");
		}
		return flashToReturn;
	}
	
	/**
	 * this method is in charge of creating a message into data base
	 * it is the default message to be stored into a row
	 * @param messageString
	 * @param user
	 * @return FlashMessage with the result of the action
	 */
	public static FlashMessage send(String messageString, User user){
		FlashMessage flashToReturn = new FlashMessage();
		if(ParametersTool.testNullObjectBoolean(user)
				&& ParametersTool.checkEmptyStringBolean(messageString)){
			Message message = new Message(messageString, user);
			send(message);
		}else{
			flashToReturn.setData(FlashMessage.Type.ERROR, "Message or user can't be null");
		}
		return flashToReturn;
		
	}
	/**
	 * overloaded Version of send message, to get string parameters
	 * @param messageString
	 * @param userId
	 * @return
	 */
	public static FlashMessage send(String messageString, long userId){
		UserTable table = new UserTable();
		User user = table.retrieveById(userId);
		return send(messageString, user);
	}
	
	/**
	 * retrieves all messages from one given user.
	 * TODO: filters this methods in table classes
	 * @param userId
	 * @return All messages from one given user id
	 */
	public static List<Message> getAllMessageFromOneUser(long userId){
		UserTable table = new UserTable();
		User user = table.retrieveById(userId);
		MessageTable msjTable = new MessageTable();
		List<Message> allMessage = msjTable.retrieveAllByUser(user);
		return allMessage;
	}
	
	
	public FlashMessage send(String subject, String message,
			long parentId, long userFromId, long userToId, int state,
			long createdAtLong, double latitud, double longitud) {
		
		User userFrom = new User(); //get from data base
		User userTo = new User(); //get from data base
		Calendar createdAt = Calendar.getInstance();
		createdAt.setTimeInMillis(createdAtLong);
		Message parentMessage = new Message(); //get from data base
		
		UserLocation location = new UserLocation();
		location.setUserLocation(latitud,longitud);
		
		return new FlashMessage();
	}
	
	/**
	 * <b>Important</b>
	 * this method is in charge of retrieving all messages from a given user
	 * it makes all kind of repositories queries to get messages sent by
	 * users into the owner scope area.
	 * @param owner
	 * @return List<Message> list of messages from user in the area
	 */
	public List<Message> getAllMessagesForAUser(User owner){
		//check parameter
		if(ParametersTool.testNullObjectBoolean(owner)){
			
			List<Message> allMyMessages = new ArrayList<Message>();
			//getUsers in owner scope
			List<User> usersInScope = new ArrayList<User>();
			ScopeAreaTable table = new ScopeAreaTable();
			List<ScopeArea> areas = table.retriveAllAreas(owner);
			
			for(ScopeArea oneArea : areas){
				usersInScope.add(oneArea.getUserOnDistance());
			}
			
			MessageTable messageTable = new MessageTable();
			//get messages sent by all users in scope area
			for(User userInScope : usersInScope){
				List<Message> messages = messageTable.retrieveAllByUser(userInScope);
				for(Message message : messages){
					allMyMessages.add(message);
				}
			}
			
			return allMyMessages;
		
		}
		return null;
	}
	/**
	 * Overloaded version of getAllMessagesForAUser method
	 * @param ownerId
	 * @return List<Message> a list with all messages
	 */
	public List<Message> getAllMessagesForAUser(long ownerId){
		if(ownerId > 0){
			UserTable table = new UserTable();
			User owner = table.retrieveById(ownerId);
			return getAllMessagesForAUser(owner);
		}
		return null;
	}
	
}
